/*
 * This file is part of OpenTTD.
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 */

/** @file getoptdata.cpp Library for parsing command line options. */

#include "../stdafx.h"
#include "getoptdata.h"

#include "../safeguards.h"

/**
 * Find the next option.
 * @return Function returns one
 * - An option letter if it found another option.
 * - -1 if option processing is finished. Inspect #argv and #numleft to find the command line arguments.
 * - -2 if an error was encountered.
 */
int GetOptData::GetOpt()
{
	// 配置数据
	const OptionData *odata;

	char *s = this->cont; // 记录配置读取位置
	if (s == nullptr) { // 如果读取位置为空,那么要按规则去查找
		// 没有剩余的了，读完了,这个numleft是在对象初始化的时候赋值的
		if (this->numleft == 0) return -1; // No arguments left -> finished.
		// 取一个字符
		s = this->argv[0];
		// 开头字符不是 '-'，那就无效了,代表读完了 
		if (*s != '-') return -1; // No leading '-' -> not an option -> finished.
		// 移动字符参数指针位置
		this->argv++;
		// 减少剩余数目
		this->numleft--;

		/* 是不是一个长选项配置  */
		for (odata = this->options; odata->flags != ODF_END; odata++) {
			if (odata->longname != nullptr && !strcmp(odata->longname, s)) {
				// 长选项使用整个参数
				this->cont = nullptr;
				// 跳转到参数处理
				goto set_optval;
			}
		}

		s++; // 跳过字符‘-’. 
	}

	/* 短项配置处理 */
	for (odata = this->options; odata->flags != ODF_END; odata++) {
		// 对比是不是这个配置.ascii码的对比 
		if (odata->shortname != '\0' && *s == odata->shortname) {
			// 调整下一个配置读取位置
			this->cont = (s[1] != '\0') ? s + 1 : nullptr;

set_optval: // Handle option value of *odata .//参数处理
			this->opt = nullptr; //配置内容置空
			switch (odata->flags) {
				case ODF_NO_VALUE: //没有附加值的配置,直接返回
					return odata->id;

				case ODF_HAS_VALUE:
				case ODF_OPTIONAL_VALUE:
					// 有选项值,把选项值赋值给opt,将下一个开始位置置空,然后配置ID
					if (this->cont != nullptr) { // Remainder of the argument is the option value.
						this->opt = this->cont;
						this->cont = nullptr;
						return odata->id;
					}
					/* No more arguments, either return an error or a value-less option. */
					/*必有选项值的配置选项,没有找到选项值,直接返回-2错误 */
					if (this->numleft == 0) return (odata->flags == ODF_HAS_VALUE) ? -2 : odata->id;

					/* Next argument looks like another option, let's not return it as option value. */
					/* 选项值可选的配置,下一个参数字符开始是新的配置选项了,代表本选项配置没有附加选项,直接返回ID*/
					if (odata->flags == ODF_OPTIONAL_VALUE && this->argv[0][0] == '-') return odata->id;

					// 读取配置值,这里如果是长类型那边goto过来的,就会在这里处理选项
					this->opt = this->argv[0]; // Next argument is the option value. 下一个参数是配置选项
					this->argv++; // 指针后移
					this->numleft--; // 剩余数目减少
					return odata->id; // 返回配置ID

				default: NOT_REACHED();
			}
		}
	}

	return -2; // No other ways to interpret the text -> error. 无法解释文本,返回错误 -2
}

